package demo2.state;

import demo2.Order;
import demo2.state.impl.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ZhouWeiBo
 * @create 2022/6/6
 */
public class OrderStateHandler {
    private Map<Integer, OrderState> stateMap = new ConcurrentHashMap<>();


    public OrderStateHandler() {
        /**
         * 以下new实例操作为练习，真实开发环境一定是从Spring容器种获取单例
         */
        stateMap.put(0, new CreateState());
        stateMap.put(1, new WaitForPayState());
        stateMap.put(2, new PaySuccessState());
        stateMap.put(3, new PayFailState());
        stateMap.put(4, new FinishState());
    }

    public OrderState stateHandler(Order order){
        OrderState orderState = stateMap.get(order.getState());
        if (orderState == null){
            throw new RuntimeException("无效的订单状态");
        }
        return orderState;
    }

}
